from django.shortcuts import render
from .models import ProvinceModel,AreaModel,CityModel
from department.models import HospitalModel,DoctorModel
from .serial import SearchDoctorListSerializer,DoctorHomeDataSerializer
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from django.db.models import Q
# Create your views here.


class Page(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    max_page_size = 10

class SearchDoctorListView(ListAPIView):
    pagination_class = Page
    serializer_class = SearchDoctorListSerializer


    def get_queryset(self):
        department = self.request.query_params.get('department')
        data = HospitalModel.objects.filter(department__name=department)
        print(data)
        province = self.request.query_params.get('province')
        print(province)
        city = self.request.query_params.get('city')
        print(city)
        if province == city:
            data = data.filter(province__name=province)
        elif city[-1] == '区':
            data = data.filter(Q(area__name=city) & Q(city__name=province))
        elif city == "全国":
            hot_city = ['北京', '上海', '广州', '杭州', '成都', '西安', '武汉']
            data = data.filter(city__name__in=hot_city)
        else:
            data = data.filter(city__name=city)

        data = DoctorModel.objects.filter(hospital__in=data)
        data = data.filter(department__name=department)
        print(data)
        price = self.request.query_params.get('price',None)
        if price == "19-39":
            data = data.filter(price__range=(19, 39))
        elif price == "40-69":
            data = data.filter(price__range=(40, 69))
        elif price == "70-99":
            data = data.filter(price__range=(70, 99))
        elif price == "100以上":
            data = data.filter(price__gte=100)

        print(data)
        title = self.request.query_params.get('title', None)
        if title != "" and title is not None:
            data = data.filter(title__name=title)
        print(data)

        status = self.request.query_params.get('status',None)
        if status is None:
            return data.order_by('id')
        print(status)
        if status == "回答次数":
            print(1111)
            return data.order_by('-reply')
        if status == "好评优先":
            return data.order_by('-rate')
        if status == "价格从低到高":
            return data.order_by('price')
        if status == "价格从高到低":
            return data.order_by('-price')
        if status == "响应时间":
            return data.order_by('reply_time')
        return data.order_by('id')



class DoctorHomeDataView(APIView):

    def get(self,request,*args,**kwargs):
        id = self.kwargs.get('id')
        data = DoctorModel.objects.filter(id=id).first()
        dat = DoctorHomeDataSerializer(data)
        return Response({'data':dat.data})
















